package com.ztjy.market.biz.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztjy.bean.Orderdetails;
import com.ztjy.bean.Orders;
import com.ztjy.market.biz.OrderdetailsBiz;
import com.ztjy.market.biz.OrdersBiz;
import com.ztjy.market.dao.OrderdetailsDao;
import com.ztjy.market.dao.OrdersDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;

@Service
public class OrdersBizImpl implements OrdersBiz {
    @Autowired
    private OrdersDao ordersDao;
    @Autowired
    private OrderdetailsDao oddao;
    @Autowired
    private OrderdetailsBiz odbiz;
    @Override
    public PageInfo<Orders> findAllOrders(Orders orders, Integer pageNo, Integer pageSize, Date ordertimeMin, Date ordertimeMax, Double ordermoneyMin, Double ordermoneyMax) {
        PageHelper.startPage(pageNo,pageSize);
        List<Orders> allOrders = ordersDao.findAllOrders(orders,ordertimeMin,ordertimeMax,ordermoneyMin,ordermoneyMax);
        PageInfo<Orders> pageInfo = new PageInfo<Orders>(allOrders);
        return pageInfo;
    }

    @Override
    public boolean addOrders(Orders orders,List<Orderdetails> orderdetails) {
        boolean flag=false;
        try {
            flag=ordersDao.addOrders(orders);
            boolean a=flag;
            for (Orderdetails od:orderdetails){
                od.setOrderId(orders.getOrderid());
                flag=oddao.addOrderdetails(od);
                if(!flag){
                    break;
                }
            }
            flag=a;
        }catch (Exception e){
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }

    @Override
    public Orders findOrderDetails(String orderid) {
        return ordersDao.findOrderDetails(orderid);
    }

    @Override
    public boolean updateOrders(Orders orders) {
        return ordersDao.updateOrders(orders);
    }

    @Override
    public boolean updateOrdersAndOrdersDetails(Orders orders, List<Orderdetails> orderdetails) {
        boolean flag=false;
        try {
            flag=oddao.delDetailsOrdersId(orders.getOrderid());
            flag=ordersDao.updateOrders(orders);
            flag=odbiz.addOrderdetails(orderdetails);
        }catch (Exception e){
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }

    @Override
    public boolean updateStatus(String orderid, Integer uid) {
        Orders orders =ordersDao.findOrderById(orderid);
        orders.setStatus("1");
//        orders.setCheckid(uid);
//        orders.setChectime(new Date(System.currentTimeMillis()));
        boolean flag=false;
        try {
            flag=ordersDao.updateOrders(orders);
        }catch (Exception e){
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }

    @Override
    public boolean delOrders(String orderid) {
        boolean flag=false;
        try {
          flag=ordersDao.delOrders(orderid);
          boolean aa=flag;
          flag=oddao.delDetailsOrdersId(orderid);
          flag=aa;
        }catch (Exception e){
            e.printStackTrace();
            flag=false;
        }
        return flag;
    }

    @Override
    public Orders findOrderById(String orderid) {
        return ordersDao.findOrderById(orderid);
    }

    @Override
    public boolean editOrders(Orders orders) {
        return ordersDao.updateOrders(orders);
    }

}
